///<reference path="../typings/index.d.ts"/>
import socketIOClient = require("socket.io-client");
import jin10SocketClient = require("socket.io-client");
import { Models } from "../QuantService/Models/Tickdata";
import http = require("http");
import fs = require("fs");
import path = require("path");
import express = require("express");
var protobuf = require('protocol-buffers');
var message = protobuf(fs.readFileSync(path.resolve(__dirname + "/MT4Quota.proto")));
export interface TickdataCallback {
    (data: Models.TickData): void;
}
export class FXCMSocketIO {
  //  private socketURL: string = "https://onlinetrade.followme.com/token=KeRv%2F3FWhBVKoIs3n2Vz13bN0%2FRSf3PN&EIO=3&transport=websocket";
  private socketURL:string = "http://onlinetrade.demo.followme.com";
   // public tickDataCallback: (data: Models.TickData) => void;
    public connectToServer(callback:TickdataCallback) {
        var socket = socketIOClient.connect(this.socketURL);

        socket.on("q", (data) => {
           var json = message.MT4Quota.decode(data);
          //  console.log(json);
            if (json.s == "XAU/USD") {
                if(callback != null){
                    var tickData = new Models.TickData();
                    tickData.ask = json.a;
                    tickData.bid = json.b;
                    tickData.symbol = json.s;
                    tickData.time = json.t;
                    callback(tickData);
                }
            }
        });
        socket.on("disconnect", () => {
            console.log("fxcm data disconnected");
        });
        socket.on("connect", () => {
            console.log("fxcm data connected ");
        });
        socket.on("error", () => {
            console.log("fxcm data socket error");
        });
    }
    constructor() {
        this.initServer();
    }
    private initServer() {
        const app = express();
        app.listen(80);
        app.get("/gold", (req, res) => {

        });

    }

}
class jin10Data {
    private jin10Url: string = "sshibkhlim.jin10.com:8085";
    connectToServer() {
        const socket = jin10SocketClient.connect(this.jin10Url);
        socket.on("price list", (data) => {
            //   console.log(data);
            if (data.name == "DXY") {
                console.log("current 美元价格:" + data.v.lp);
            }
        });
        socket.on("connect", () => {
            console.log("jin10 socket connected");
            socket.emit("reg", "ok");
            socket.emit("reqvote", "ok");
        });
        socket.on("time", (time) => {
            // console.log("jin10 time:" + time);
        });
        socket.on("error", (e) => {
            console.log("jin10 scoket error:" + e);
        });
        socket.emit("delAllSubscription", []);
        socket.emit("addSubscription", ["XAUUSD", "DXY"]);

    }
}

export class FXCMData {
    getFXCMQuote(): void {
        new FXCMSocketIO();
    }
}

//new FXCMData().getFXCMQuote();
//new jin10Data().connectToServer();